/* 
 *  <copyright file="AnimatedTexture.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections;
using System.Collections.Generic;

using Microsoft.Xna.Framework;

using SobrietyEngine.Assets.Textures;

namespace SobrietyEngine.Components.Graphic
{
    public class AnimatedSpriteCmp:Sprite2DCmp, SobrietyEngine.Components.IComponent
    {

        protected AnimationData animation = null;
        protected float nextUpdate = 0.0f;
        protected String currentAnimation = "";
        protected int currentFrame = 0;
        protected List<FrameData> currentFrames = null;
        protected String animationSet="";

        protected float animationSpeedMult = 1.0f;
        public float AnimationSpeedMult { get { return animationSpeedMult; } set { animationSpeedMult = value; } }

        protected Boolean enabled = true;
        public Boolean Enabled { get { return enabled; } set { enabled = value; } }

        protected Boolean reverse = true;
        public Boolean Reverse { get { return reverse; } set { reverse = value; } }

        public AnimatedSpriteCmp():base()
        {

        }

         override public Boolean InitComponent()
        {

            animation = Actor.GameServices().TextureGameService.GetAnimation(animationSet);
            if(animation==null)
                throw new Exception("couldnt find animation set");

            Actor.GameServices().TextureGameService.LoadAnimationTextures(animationSet);

            currentFrames = animation.Animations[currentAnimation];
            currentFrame = 0;
            nextUpdate = ((FrameData)currentFrames[currentFrame]).Time;

            //if we dont set the textrurename, base gets unhappy
            TextureName = ((FrameData)currentFrames[currentFrame]).TextureName;
            base.InitComponent();

            return true;
        }

        public void SetAnimation(String animationName)
        {
            if (currentAnimation == animationName)
                return;
            currentAnimation = animationName;
            currentFrames = animation.Animations[animationName];
            currentFrame = 0;
            nextUpdate = ((FrameData)currentFrames[currentFrame]).Time;
        }

        public void SetFrame(int frameNum)
        {
            currentFrame = frameNum;
        }

        public new void Update(GameTime elapsedTime)
        {
            nextUpdate -= (elapsedTime.ElapsedGameTime.Milliseconds/1000.0f) * animationSpeedMult;
            while (nextUpdate <= 0)
            {
                if (Reverse)
                    ++currentFrame;
                else
                    --currentFrame;
                if (currentFrame >= currentFrames.Count)
                    currentFrame = 0;
                else if (currentFrame < 0)
                    currentFrame = currentFrames.Count - 1;

                nextUpdate += ((FrameData)currentFrames[currentFrame]).Time;
            }

            spriteTexture = ((FrameData)currentFrames[currentFrame]).Texture;

            if (spriteTexture.Width > spriteTexture.Height)
                boundingRadius = spriteTexture.Width;
            else
                boundingRadius = spriteTexture.Height;
        }

        public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            //spriteTexture = ((FrameData)currentFrames[currentFrame]).Texture;
            base.Draw(spriteBatch);
        }
    }
}